﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class ZonasDA
    {
        private ZONASTableAdapter Adapter = new ZONASTableAdapter();
        private DataSet.ZONASDataTable entities;
        private DataSet.ZONASRow entity;

        public decimal Insert(ZONAS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.ZONASDataTable();

                    entity = entities.NewZONASRow();

                    entity.ID = value.ID;
                    entity.ZONA = value.ZONA.Trim();
                    entity.COMENTARIO = value.COMENTARIO;

                    entities.AddZONASRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(ZONAS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();


                    entity.ZONA = value.ZONA.Trim();
                    entity.COMENTARIO = value.COMENTARIO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public ZONAS GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    ZONAS zona;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        zona = new ZONAS
                            {
                                ID = entity.ID,
                                ZONA = entity.ZONA,
                                COMENTARIO = entity.COMENTARIO
                            };
                    }
                    else
                        zona = null;

                    return zona;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<ZONAS> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<ZONAS> lst = new List<ZONAS>();
                    foreach (DataSet.ZONASRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new ZONAS
                        {
                            ID = rw.ID,
                            ZONA = rw.ZONA,
                            COMENTARIO = rw.COMENTARIO
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<ZONAS> GetByDescripcion(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<ZONAS> lst = new List<ZONAS>();
                    foreach (DataSet.ZONASRow rw in this.Adapter.GetDataByZona(value.Trim()))
                    {
                        lst.Add(new ZONAS
                        {
                            ID = rw.ID,
                            ZONA = rw.ZONA,
                            COMENTARIO = rw.COMENTARIO
                        });
                    }
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }
    }
}
